<template>
	<view>
		<view class="fileUploadBox">
			<view class="fileUploadBlock">
				<view class="text">
					<view class="label">{{config.name}}</view>
					<view class="subTitle">点击上传{{config.name}}</view>
				</view>
				<u-upload :action="action" :custom-btn="true" :before-upload="beforeUpload"
					@on-uploaded="contractUploadOver" :show-upload-list="ishowUploadList" ref="uUpload"
					name="uploadFile">
					<view slot="addBtn" class="upload">
						<img :src='file == "" ? defaultUrl:file' alt="" width="100%">
					</view>
				</u-upload>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			value: {
				type: String,
				default: ""
			},
			config: {
				type: Object,
				default: () => {}
			},
		},
		model: {
			prop: "value",
			event: "change"
		},
		computed: {
			file: {
				get() {
					return this.value || "";
				},
				set(val) {
					this.$emit('change', val);
				}
			}
		},
		data() {
			return {
				action: "",
				defaultUrl: ""
			}
		},
		onReady() {
			switch (this.config.key) {
				case "certificate":
					this.defaultUrl = "https://supply.zhongyimotor.com/upload/applet/yingyezhizhao@3x.png"
					break;
				case "legalIdCardFront":
				case "financeIdCardFront":
					this.defaultUrl = "https://supply.zhongyimotor.com/upload/applet/renxiangmian@2x.png"
					break;
				case "legalIdCardBack":
				case "financeIdCardBack":
					this.defaultUrl = "https://supply.zhongyimotor.com/upload/applet/guohuimian@2x.png"
					break;
			}

			this.action = this.$config.uploadFileUrl
		},
		methods: {
			beforeUpload() {
				uni.showLoading({
					title: '上传中'
				})
			},
			contractUploadOver(lists, name) {
				this.$refs.uUpload.clear()
				this.file = lists[lists.length - 1].response.result.url
				uni.hideLoading()
			}
		}
	}
</script>

<style scoped>
	.fileUploadBox {
		margin-left: -30rpx;
	}
</style>